﻿using Bottle.CommunityForum.IBLL;
using Bottle.CommunityForum.Model;
using Bottle.CommunityForum.Model.Enum;
using Bottle.CommunityForum.Model.Param;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Bottle.CommunityForum.BLL
{
    public partial class UserInfoService : BaseService<UserInfo>, IUserInfoService
    {

        public IQueryable<UserInfo> QueryWhereLoadPage<S>(UserInfoParam queryUserParam, Expression<Func<UserInfo, S>> ordlambda)
        {
            int delNormal = (int)UserEnum.StateNormal;
            var Param = Db.UserInfoDal.QueryEntitys(u => u.state == delNormal);

            if (!string.IsNullOrEmpty(queryUserParam.Keyword))
            {
                Param = Param.Where(u => u.username.Contains(queryUserParam.Keyword) || u.userpwd.Contains(queryUserParam.Keyword) || u.email.Contains(queryUserParam.Keyword))
                    .AsQueryable();
            }

            if (!string.IsNullOrEmpty(queryUserParam.username))
            {
                Param = Param.Where(u => u.username.Contains(queryUserParam.username))
                    .AsQueryable();
            }

            if (!string.IsNullOrEmpty(queryUserParam.userpwd))
            {
                Param = Param.Where(u => u.userpwd.Contains(queryUserParam.userpwd))
                    .AsQueryable();
            }

            if (!string.IsNullOrEmpty(queryUserParam.email))
            {
                Param.Where(u => u.email.Contains(queryUserParam.email))
                    .AsQueryable();
            }

            queryUserParam.totle = Param.Count();

            if (queryUserParam.isOrder)
            {
                Param = Param.OrderBy(ordlambda)
                    .Skip(queryUserParam.pageSize * (queryUserParam.pageIndex - 1))
                    .Take(queryUserParam.pageSize)
                    .AsQueryable();
            }
            else
            {
                Param = Param.OrderByDescending(ordlambda)
                    .Skip(queryUserParam.pageSize * (queryUserParam.pageIndex - 1))
                    .Take(queryUserParam.pageSize)
                    .AsQueryable();
            }

            return Param;
        }
        /// <summary>
        /// 设置用户角色
        /// </summary>
        /// <param name="model"></param>
        /// <param name="roleList"></param>
        /// <returns></returns>
        public bool SetRole(UserInfo model, List<int> roleList)
        {
            var UserModel = Db.UserInfoDal.QueryEntitys(u => u.userid == model.userid).FirstOrDefault();

            UserModel.username = model.username;
            UserModel.userpwd = model.userpwd;
            UserModel.email = model.email;

            var roleAll = Db.RoleInfoDal.QueryEntitys(r => roleList.Contains(r.roleid));

            UserModel.role.Clear();

            foreach (var role in roleAll)
            {
                UserModel.role.Add(role);
            }

            Db.UserInfoDal.EditEntity(UserModel);
            return Db.SaveChanges() > 0;
        }
    }
}
